package tree;

/**
 * 根据前序中序，生成树
 */
public class TreeCreator {

  public TreeNode createTree(String preStr, String inStr) {
    if (preStr == null || inStr == null || preStr.isEmpty() || inStr.isEmpty()) {
      return null;
    }
    char rootValue = preStr.charAt(0);
    int rootIndex = inStr.indexOf(rootValue);
    TreeNode root = new TreeNode(rootValue);
    root.setLeft(createTree(preStr.substring(1, rootIndex + 1), inStr.substring(0, rootIndex)));
    root.setRight(createTree(preStr.substring(rootIndex + 1), inStr.substring(rootIndex + 1)));
    return root;
  }


  public static void main(String args[]) {
    TreeCreator creator = new TreeCreator();
    TreeIterator iterator = new TreeIterator();
    TreeNode root = creator.createTree("ABDEGCF", "DBGEACF");
    System.out.println("先序遍历");
    iterator.preOrder(root);
    System.out.println();
    System.out.println("中序遍历");
    iterator.inOrder(root);
    System.out.println();
    System.out.println("后序遍历");
    iterator.postOrder(root);

  }
}
